/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MenuEscalera.java
 *
 * Created on 05-jun-2012, 17:22:18
 */

package com.cmf.vista;

import com.cmf.controladores.Funciones;
import com.cmf.model.Jugador;
import com.cmf.model.Model;
import java.awt.Color;
import java.awt.event.KeyEvent;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JFrame;

/**
 *
 * @author jairo jose
 */
public class MenuEscalera extends javax.swing.JFrame {

    /** Creates new form MenuEscalera */
    private static Model model;
    public MenuEscalera() {
        
        initComponents();
        Fondo fondo1=new Fondo("32.gif");
        fondoP.setBorder(fondo1.getBorder());
        ButtonGroup bg=new ButtonGroup();
        bg.add(escalera);
        bg.add(puntos);
        ImageIcon imagen=new ImageIcon(getClass().getResource("/com/cmf/recursos/imagenes/claret.JPG"));
        setIconImage(imagen.getImage());
        
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        fondoP = new javax.swing.JPanel();
        njugadores = new javax.swing.JSpinner();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        seg = new javax.swing.JSpinner();
        jLabel3 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        j1 = new javax.swing.JTextField();
        j2 = new javax.swing.JTextField();
        j3 = new javax.swing.JTextField();
        j4 = new javax.swing.JTextField();
        jugar = new javax.swing.JButton();
        escalera = new javax.swing.JRadioButton();
        puntos = new javax.swing.JRadioButton();
        jLabel4 = new javax.swing.JLabel();
        npuntos = new javax.swing.JSpinner();
        jMenuBar1 = new javax.swing.JMenuBar();
        juegoMenu = new javax.swing.JMenu();
        PreguntasMenu = new javax.swing.JMenu();
        Agregar = new javax.swing.JMenuItem();
        creditos = new javax.swing.JMenuItem();
        Ayuda = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        njugadores.setModel(new javax.swing.SpinnerNumberModel(1, 1, 4, 1));
        njugadores.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                njugadoresStateChanged(evt);
            }
        });
        njugadores.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                njugadoresKeyReleased(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("Numero de jugadores:");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setText("Tiempo para contestar preguntas:");

        seg.setModel(new javax.swing.SpinnerNumberModel(30, 1, 60, 1));

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(255, 255, 255));
        jLabel3.setText("Segundos");

        jLabel11.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel11.setForeground(new java.awt.Color(255, 255, 255));
        jLabel11.setText("jugador 4");

        jLabel10.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel10.setForeground(new java.awt.Color(255, 255, 255));
        jLabel10.setText("Jugador 3");

        jLabel9.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel9.setForeground(new java.awt.Color(255, 255, 255));
        jLabel9.setText("Jugador 2");

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel8.setForeground(new java.awt.Color(255, 255, 255));
        jLabel8.setText("Jugador 1");

        j1.setText("Jugador 1");

        j2.setText("Jugador 2");
        j2.setEnabled(false);
        j2.setOpaque(false);

        j3.setText("Jugador 3");
        j3.setEnabled(false);
        j3.setOpaque(false);

        j4.setText("Jugador 4");
        j4.setEnabled(false);
        j4.setOpaque(false);

        jugar.setBackground(new java.awt.Color(255, 255, 0));
        jugar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/cmf/recursos/imagenes/juego.png"))); // NOI18N
        jugar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jugarMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jugarMouseExited(evt);
            }
        });
        jugar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jugarActionPerformed(evt);
            }
        });
        jugar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jugarKeyReleased(evt);
            }
        });

        escalera.setBackground(new java.awt.Color(255, 0, 0));
        escalera.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        escalera.setForeground(new java.awt.Color(255, 255, 255));
        escalera.setSelected(true);
        escalera.setText("Escalera");

        puntos.setBackground(new java.awt.Color(255, 0, 0));
        puntos.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        puntos.setForeground(new java.awt.Color(255, 255, 255));
        puntos.setText("Puntos");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(255, 255, 255));
        jLabel4.setText("Tipo de Juego");

        npuntos.setModel(new javax.swing.SpinnerNumberModel(10, 1, 30, 1));
        npuntos.setName(""); // NOI18N

        javax.swing.GroupLayout fondoPLayout = new javax.swing.GroupLayout(fondoP);
        fondoP.setLayout(fondoPLayout);
        fondoPLayout.setHorizontalGroup(
            fondoPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(fondoPLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(fondoPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(fondoPLayout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(njugadores, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(fondoPLayout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(seg, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel3))
                    .addGroup(fondoPLayout.createSequentialGroup()
                        .addGroup(fondoPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel9))
                        .addGap(45, 45, 45)
                        .addGroup(fondoPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(j2)
                            .addComponent(j1)))
                    .addGroup(fondoPLayout.createSequentialGroup()
                        .addGroup(fondoPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel10)
                            .addComponent(jLabel11)
                            .addComponent(jLabel4))
                        .addGap(18, 18, 18)
                        .addGroup(fondoPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(j4)
                            .addComponent(j3)
                            .addGroup(fondoPLayout.createSequentialGroup()
                                .addGroup(fondoPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jugar, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(fondoPLayout.createSequentialGroup()
                                        .addComponent(escalera)
                                        .addGap(18, 18, 18)
                                        .addComponent(puntos)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(npuntos, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(0, 0, Short.MAX_VALUE)))))
                .addGap(18, 18, 18))
        );
        fondoPLayout.setVerticalGroup(
            fondoPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(fondoPLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(fondoPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(njugadores, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(fondoPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addGroup(fondoPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(seg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel3)))
                .addGap(18, 18, 18)
                .addGroup(fondoPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(j1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(fondoPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(j2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(fondoPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(j3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(fondoPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(j4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(fondoPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(npuntos)
                    .addGroup(fondoPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(escalera)
                        .addComponent(puntos)
                        .addComponent(jLabel4)))
                .addGap(18, 18, 18)
                .addComponent(jugar, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(29, 29, 29))
        );

        juegoMenu.setText("Juego");

        PreguntasMenu.setText("Preguntas");

        Agregar.setText("Agregar");
        Agregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AgregarActionPerformed(evt);
            }
        });
        PreguntasMenu.add(Agregar);

        juegoMenu.add(PreguntasMenu);

        creditos.setText("Creditos");
        juegoMenu.add(creditos);

        Ayuda.setText("Ayuda");
        juegoMenu.add(Ayuda);

        jMenuBar1.add(juegoMenu);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(fondoP, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(fondoP, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        setSize(new java.awt.Dimension(407, 441));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void AgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AgregarActionPerformed
        // TODO add your handling code here:
        AgregarPreguntas ap=new AgregarPreguntas(this);
        ap.setVisible(true);
        dispose();
    }//GEN-LAST:event_AgregarActionPerformed

    private void njugadoresStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_njugadoresStateChanged
        // TODO add your handling code here:
        String valor=njugadores.getValue().toString();
        if(valor.equals("1")){
            j2.setOpaque(false);
            j2.setEnabled(false);
            j3.setOpaque(false);
            j3.setEnabled(false);
            j4.setOpaque(false);
            j4.setEnabled(false);
        }else
            if(valor.equals("2")){
                j2.setOpaque(true);
                j2.setEnabled(true);
                j3.setOpaque(false);
                j3.setEnabled(false);
                j4.setOpaque(false);
                j4.setEnabled(false);
            }else
                if(valor.equals("3")){
                    j3.setOpaque(true);
                    j3.setEnabled(true);
                    j4.setOpaque(false);
                    j4.setEnabled(false);
                }else
                    if(valor.equals("4")){
                        j4.setOpaque(true);
                        j4.setEnabled(true);
                    }
    }//GEN-LAST:event_njugadoresStateChanged

    private void jugarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jugarActionPerformed
        // TODO add your handling code here:
        iniciarjuego();
    }//GEN-LAST:event_jugarActionPerformed

    private void jugarMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jugarMouseEntered
        // TODO add your handling code here:
        jugar.setBackground(Color.BLUE);
    }//GEN-LAST:event_jugarMouseEntered

    private void jugarMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jugarMouseExited
        // TODO add your handling code here:
        jugar.setBackground(Color.yellow);
    }//GEN-LAST:event_jugarMouseExited

    private void jugarKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jugarKeyReleased
       iniciarjuego();
    }//GEN-LAST:event_jugarKeyReleased

    private void njugadoresKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_njugadoresKeyReleased
        // TODO add your handling code here:
        if(evt.getKeyCode()==KeyEvent.VK_ENTER)
            jugarKeyReleased(evt);
    }//GEN-LAST:event_njugadoresKeyReleased

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MenuEscalera().setVisible(true);

            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem Agregar;
    private javax.swing.JMenuItem Ayuda;
    private javax.swing.JMenu PreguntasMenu;
    private javax.swing.JMenuItem creditos;
    private javax.swing.JRadioButton escalera;
    private javax.swing.JPanel fondoP;
    private javax.swing.JTextField j1;
    private javax.swing.JTextField j2;
    private javax.swing.JTextField j3;
    private javax.swing.JTextField j4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenu juegoMenu;
    private javax.swing.JButton jugar;
    private javax.swing.JSpinner njugadores;
    private javax.swing.JSpinner npuntos;
    private javax.swing.JRadioButton puntos;
    private javax.swing.JSpinner seg;
    // End of variables declaration//GEN-END:variables

    private void iniciarjuego() {
        //captura el numero de jugadores que van a jugar+
        int index=Integer.parseInt(njugadores.getValue().toString());
        model=Model.getModel();
        //crea los jugadores y asigna el nombre a cada uno
        Jugador []j=new Jugador[index];
        for (int i=0; i<index; i++){
            if(i==0){
                j[i]=new Jugador(j1.getText());
            }
            else
                if(i==1)
                    j[i]=new Jugador(j2.getText());
                else
                    if(i==2)
                        j[i]=new Jugador(j3.getText());
                    else 
                        if(i==3)
                            j[i]=new Jugador(j4.getText());
            j[i].setPreguntas(model.getPreguntas());
        }
        Funciones f=new Funciones();// objeto de la clase de funciones
        int turno=f.azar(j.length);//seleccion del turno por medio del metodo azar
        j[turno-1].setTurno(true);//asignacion del turno al jugador
        int temp=Integer.parseInt(seg.getValue().toString());//captura del tiempo para contestar cada pregunta
        int ptoMax=Integer.parseInt(npuntos.getValue().toString());//captura de la cantidad maxima de puntos para competir
        if(escalera.isSelected()==true)//condicion para escoger el modo de juego ("Escalera" o "Puntos")
            new EscaleraClaret(j,temp,true,ptoMax).setVisible(true);//si es seleccionado el modo de "Escalera"
        else
            new EscaleraClaret(j,temp,false,ptoMax).setVisible(true);//sino es seleccionado el modo de "Escalera"
        dispose();
    }

}
